﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;

namespace JuCloudy.Data.Nhibernate.App.Toursim
{
    public class ItineraryContainer
    {
        private static global::NHibernate.Cfg.Configuration mCfg = null;
        private static global::NHibernate.ISessionFactory SessionFactory = null;
        private AppContainer<JuCloudy.Data.Entity.AppToursimItinerary> app { get; set; }
        public String Insert(Entity.AppToursimItinerary Entity)
        {
            mCfg = new NHibernate.Cfg.Configuration().Configure(
               System.Web.HttpContext.Current.Server.MapPath(
                    System.Configuration.ConfigurationManager.AppSettings["AppToursimNhCfg"]
                    ));

            SessionFactory = null;

            mCfg.AddClass(typeof(JuCloudy.Data.Entity.AppToursimItinerary));
            SessionFactory = mCfg.BuildSessionFactory();

            Entity.CreateDate = DateTime.Now;

            using (NHibernate.ISession session = SessionFactory.OpenSession())
            {
                var query = from c in session.Query<JuCloudy.Data.Entity.AppToursimItinerary>()
                            where c.Detial==Entity.Detial
                            select c;
                if(query!=null&&query.Count()>0)
                {
                    return query.ToList()[0].ItineraryId.ToString();
                }
                else
                {
                    using (NHibernate.ITransaction vTransaction = session.BeginTransaction())
                    {
                        try
                        {
                            session.SaveOrUpdate(Entity);
                            vTransaction.Commit();
                            vTransaction.Dispose();
                            session.Flush();
                            session.Close();
                            session.Dispose();
                            return Entity.ItineraryId.ToString();
                        }
                        catch (Exception ex)
                        {
                            vTransaction.Rollback();
                            vTransaction.Dispose();
                            session.Flush();
                            session.Close();
                            session.Dispose();
                            throw ex;
                        }
                    }
                }
            }
        }

        public List<Entity.AppToursimItinerary> Delete(Entity.AppToursimItinerary Entity)
        {
            app = new AppContainer<Entity.AppToursimItinerary>("AppToursimNhCfg");
            return app.Delete(Entity);
        }

        public Entity.AppToursimItinerary GetEntity(string Query)
        {
            app = new AppContainer<Entity.AppToursimItinerary>("AppToursimNhCfg");
            return app.GetEntity("ItineraryId", Query);
        }

        public List<Entity.AppToursimItinerary> GetEntities()
        {
            app = new AppContainer<Entity.AppToursimItinerary>("AppToursimNhCfg");
            return app.GetEntities();
        }
    }
}
